package com.wswy.wzcx.ui.main.garage

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import com.wswy.wzcx.model.convert
import com.wswy.wzcx.model.core.EventTabConfResp
import com.wswy.wzcx.model.home.ExtModule
import com.wswy.wzcx.model.license.UserCarInfo
import com.wswy.wzcx.module.CarsViewModel
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class GarageViewModel : CarsViewModel() {


    private val tabConfLiveData = MutableLiveData<Map<String,List<ExtModule>>?>()

    // todo 优化
    //注意  此处没有考虑 ViewModel 多用情况！！！
    var carUpdateCallback : ((UserCarInfo)->Unit)?=null

    var carDeleteCallback : ((Int)->Unit)?=null

    override fun onUserCarInfoUpdate(userCarInfo: UserCarInfo) {
        super.onUserCarInfoUpdate(userCarInfo)

        carUpdateCallback?.invoke(userCarInfo)
    }

    override fun onDeleteCar(carId: Int) {
        super.onDeleteCar(carId)
        carDeleteCallback?.invoke(carId)
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun onEventTabConfResp(resp: EventTabConfResp) {

        resp.tabConfResp?.garage?.run {
            tabConfLiveData.postValue(this.convert())
        } ?:kotlin.run {
            tabConfLiveData.postValue(null)
        }

    }

    fun getTabConfLiveData():LiveData<Map<String,List<ExtModule>>?> = tabConfLiveData

    override fun onCleared() {
        super.onCleared()
        carUpdateCallback = null
        carDeleteCallback = null
    }

    override fun autoRegisterEvent()=true
}